function varargout = Lab1(varargin)
%LAB1 M-file for Lab1.fig
%      LAB1, by itself, creates a new LAB1 or raises the existing
%      singleton*.
%
%      H = LAB1 returns the handle to a new LAB1 or the handle to
%      the existing singleton*.
%
%      LAB1('Property','Value',...) creates a new LAB1 using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to Lab1_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      LAB1('CALLBACK') and LAB1('CALLBACK',hObject,...) call the
%      local function named CALLBACK in LAB1.M with the given input
%      arguments.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Lab1

% Last Modified by GUIDE v2.5 21-Mar-2012 22:40:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Lab1_OpeningFcn, ...
                   'gui_OutputFcn',  @Lab1_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
end

% --- Executes just before Lab1 is made visible.
function Lab1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for Lab1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Lab1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
formatTable([handles.uitable1, handles.uitable3], zeros(2));
end


% --- Outputs from this function are returned to the command line.
function varargout = Lab1_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
end


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiopen('load');
formatTable(handles.uitable1, cMatrix);
formatTable(handles.uitable3, dMatrix);
set(handles.edit1, 'String', rows);
set(handles.edit2, 'String', variant);
set(handles.edit3, 'String', expression);
end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cMatrix = get(handles.uitable1, 'Data');
dMatrix = get(handles.uitable3, 'Data');
rows = str2double(get(handles.edit1, 'String'));
variant = str2double(get(handles.edit2, 'String'));
expression = get(handles.edit3, 'String');
uisave({'cMatrix', 'dMatrix', 'variant', 'rows', 'expression'}, 'LabTask.mat');
end


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
rows = str2double(get(handles.edit1, 'String'));
rows = rows + 1;
set(handles.edit1, 'String', rows);
formatTable([handles.uitable1, handles.uitable3], zeros(rows, rows));
end


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
rows = str2double(get(handles.edit1, 'String'));
if rows > 2
    rows = rows - 1;
end;
formatTable([handles.uitable1, handles.uitable3], zeros(rows, rows));
set(handles.edit1, 'String', rows);
end


function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double
checkIntegerInput(hObject, 1, 15, 1);
end


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
C = get(handles.uitable1, 'Data');
D = get(handles.uitable3, 'Data');
k = str2double(get(handles.edit2, 'String'));
dim = str2double(get(handles.edit1, 'String'));
expr = get(handles.edit3, 'String');
try
    result = eval(expr);
catch err
   errordlg('Can''t evaluate expression','Bad Input','modal');
   uicontrol(handles.edit3);
   return
end
formatTable(handles.uitable4, result);
formatTable(handles.uitable6, zeros(dim, 1));
formatTable(handles.uitable7, zeros(dim, 1));
set(handles.uitable6, 'ColumnEditable', false);
end
    



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
checkIntegerInput(hObject, 2, inf, 2);
newDim = str2double(get(hObject, 'String'));
formatTable([handles.uitable1, handles.uitable3], zeros(newDim));
end

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end;
end


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A = get(handles.uitable4, 'Data');
if iscell(A)
    errordlg('Task is not initialized!','Init error','modal');
    return
end;
if (~IsSymmetric(A))
    msgbox('The matrix A is not symmetric!');
    return
end
method = get(handles.radiobutton3, 'Value');
if (method)
    [V, h, k, Ak] = JacobiRotationAlgorithm(A, 0.0001);
    str = sprintf('Iterations: %d', k);
    set(handles.listbox2, 'String', str);
else
    [V, h, k, Ak] = JacobiRotationAlgorithm2(A, 0.0001);
    str = sprintf('Iterations: %d', k);
    set(handles.listbox2, 'String', str);
end;
formatTable(handles.uitable4, Ak);
formatTable(handles.uitable6, V);
set(handles.uitable7, 'Data', h);
end


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end;
end


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
rows = str2double(get(handles.edit1, 'String'));
cols = str2double(get(handles.edit4, 'String'));
cols = cols + 1;
if cols == rows
    set(handles.pushbutton7, 'Enable', 'off');
end;
formatTable([handles.uitable1, handles.uitable3], zeros(rows, cols));
set(handles.edit1, 'String', rows);
set(handles.edit4, 'String', cols);
end

% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
rows = str2double(get(handles.edit1, 'String'));
cols = str2double(get(handles.edit4, 'String'));
if cols > 2
    cols = cols - 1;
end;
if rows > cols
    set(handles.pushbutton7, 'Enable', 'on');
end;
formatTable([handles.uitable1, handles.uitable3], zeros(rows, cols));
set(handles.edit1, 'String', rows);
set(handles.edit4, 'String', cols);
end


% --- Executes when selected object is changed in uipanel3.
function uipanel3_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel3 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
p = 3;
end
